package com.hwhhhh.mygrad;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.hwhhhh.mygrad.api.Api;
import com.hwhhhh.mygrad.api.ApiCallback;
import com.hwhhhh.mygrad.api.ApiConfig;
import com.hwhhhh.mygrad.entity.RespData;
import com.hwhhhh.mygrad.dto.UserDTO;
import com.hwhhhh.mygrad.utils.StringUtils;

public class LoadingActivity extends BaseActivity {

    @Override
    protected void initView() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_loading;
    }

    @Override
    protected void initData() {
        setBarColor(R.color.background_color);
        Api.config(ApiConfig.VERIFY_TOKEN, null).getRequest(this, new ApiCallback() {
            @Override
            public void onSuccess(String res) {
                RespData<UserDTO> data = StringUtils.fromJson(res, new TypeToken<RespData<UserDTO>>(){}.getType());
                if (data.getCode() == 200) {
                    Log.d("LoadingActivity", "onSuccess: " + data.getData().getProfilePhoto());
                    UserDTO entity = data.getData();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("user", entity);
                    navigateToWithBundle(HomeActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK, bundle);
                } else {
                    navigateToWithFlag(LoginActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    showToastSync("登录已失效，请重新登录！");
                }
            }

            @Override
            public void onFailure(Exception e) {
                navigateToWithFlag(LoginActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                showToastSync("网络异常，请重新登录！");
            }
        });
    }
}